package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goodsType;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "管理后台 - 小程序-商品分类")
@RestController
@RequestMapping("/qd/goodsType")
@Validated
public class GoodsTypeController {

    @Resource
    GoodsTypeMapper goodsTypeMapper;

    @PostMapping("/create")
    public CommonResult create(@RequestBody GoodsType dto){
        try {
            goodsTypeMapper.insert(dto);
        }catch (Exception e){
            if (e.getMessage().contains("qd_goods_type.name")){
                throw new ServiceException(500,"名称不能重复");
            }
        }
        return CommonResult.success("");
    }

    @PutMapping("/update")
    public CommonResult update(@RequestBody GoodsType dto){
        try {
            goodsTypeMapper.updateById(dto);
        }catch (Exception e){
            if (e.getMessage().contains("qd_goods_type.name")){
                throw new ServiceException(500,"名称不能重复");
            }
        }
        return CommonResult.success("");
    }

    @GetMapping("getOneLevel")
    @Operation(summary = "获取一级分类配置")
    public CommonResult getOneLevel(){
        return CommonResult.success(goodsTypeMapper.selectList(new LambdaQueryWrapper<GoodsType>().eq(GoodsType::getType,1)));
    }

    @GetMapping("getById")
    @Operation(summary = "获取分类下的子集")
    public CommonResult getById(Integer id){
        return CommonResult.success(goodsTypeMapper.selectList(new LambdaQueryWrapper<GoodsType>().eq(GoodsType::getPid,id)));
    }

    @GetMapping("getTree")
    @Operation(summary = "获取树")
    public CommonResult getTree(){
        List<TreeVo> trees=new ArrayList<>();
        List<GoodsType> goodsTypes = goodsTypeMapper.selectList();

        for (GoodsType vo : goodsTypes) {
            if (vo.getPid()==0)
                trees.add(new TreeVo(vo.getId(),vo.getName()));
        }
        trees=child(goodsTypes, trees);
        for (TreeVo tree : trees) {
            tree.setChildren(child(goodsTypes, tree.getChildren()));
        }

        return CommonResult.success(trees);
    }

    public List<TreeVo> child(List<GoodsType> goodsTypes,List<TreeVo> trees){
        for (TreeVo tree : trees) {
            List<TreeVo> child=new ArrayList<>();
            for (GoodsType goodsType : goodsTypes.stream().filter(e -> e.getPid().equals(tree.getValue())).collect(Collectors.toList())) {
                child.add(new TreeVo(goodsType.getId(),goodsType.getName()));
            }
            tree.children=child;
        }
        return trees;
    }

    @DeleteMapping("/delete")
    public CommonResult delete(Integer id) {
        GoodsType dto = goodsTypeMapper.selectById(id);
        if (dto==null)
            throw new ServiceException(500,"数据不存在,请刷新");
        goodsTypeMapper.deleteById(id);
        return CommonResult.success("");
    }


}
